package cn.com.dycc.sys_manage.post.service;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import cn.com.dycc.sys_manage.post.dao.PostDao;
import cn.com.dycc.sys_manage.post.entity.PostEntity;
import cn.com.dycc.sys_manage.post.param.FindListPostParam;
import cn.com.dycc.sys_manage.post.param.PostParam;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserChildAccountDao;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.IntegerUtil;
import cn.com.dycc.util.Paging;

@Service("postService")
@Transactional
public class PostServiceImpl implements PostService{
	
	@Resource(name="postDao")
	private PostDao postDao;
	
	@Resource(name="paging")
	private Paging paging;
	
	@Resource(name="userChildAccountDao")
	private UserChildAccountDao userChildAccountDao;
	
	//增加岗位信息 HttpServletRequest request, 
	public String addPost(HttpServletRequest request,PostParam postParam) throws PostNotFondException {
		if(postParam.getPost_name()==null||postParam.getPost_name().trim().isEmpty()){
			throw new PostNotFondException("请输入职务名称！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		//是否已存在同名的职务
		int countName = postDao.findCountPostByPostName(org_id, postParam.getPost_name());
		if(countName>=1){
			throw new PostNotFondException("添加的职务名称已存在！");
		}
		
		if(postParam.getOrder_no()!=null&&!(IntegerUtil.isNumeric(postParam.getOrder_no().toString()))){
			throw new PostNotFondException("排序类型为整型！");
		}
		
		
		String opt_user_id = request.getSession().getAttribute("id").toString();
//		String org_id = "1";
//		String opt_user_id = "1";
		if(opt_user_id==null){
			throw new PostNotFondException("请登录！");
		}
		
		postParam.setOrg_id(org_id);
		postParam.setOpt_user_id(opt_user_id);
		
		int add = postDao.addPost(postParam);
		
		if(add == 0){
			throw new PostNotFondException("增加职务失败！");
		}
		
		return "增加成功！";
	}
	
	//查询所有的岗位信息 HttpServletRequest request, 
	public List<PostEntity> findPostAll(HttpServletRequest request,FindListPostParam findListPostParam)
			throws PostNotFondException {
		Integer limit = findListPostParam.getLimit();
		Integer page = findListPostParam.getPage();
		
		if(!(IntegerUtil.isNumeric(page.toString()))){
			throw new PostNotFondException("查询页码为整型！");
		}
		
		Integer start = null;
		if(limit==null&&page!=null){
			limit = paging.getPageSize();
			start=(page-1)*limit;
		}
		if(page==null){
			start = null;
			limit = null;
		}
		if(limit!=null&&page!=null){
			start = limit*(page-1);
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		findListPostParam.setLimit(limit);
		findListPostParam.setStart(start);
		findListPostParam.setOrg_id(org_id);
		findListPostParam.setState("Y");
		
		return postDao.findPostAll(findListPostParam);
	}
	
	//更新所有岗位信息 HttpServletRequest request, 
	public String updatePost(HttpServletRequest request, PostParam postParam) throws PostNotFondException {
		if(postParam.getId()==null||postParam.getId().trim().isEmpty()){
			throw new PostNotFondException("请选择需要更新的职务！");
		}
		
		if(!(IntegerUtil.isNumeric(postParam.getId()))){
			throw new PostNotFondException("职务的id为整型！");
		}
		
		if(postParam.getPost_name()==null||postParam.getPost_name().trim().isEmpty()){
			throw new PostNotFondException("请输入职务名称！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		if(!(postParam.getPost_name().equals(postDao.findPostNameByid(postParam.getId(),org_id)))){
			//是否已存在同名的职务
			int countName = postDao.findCountPostByPostName(org_id, postParam.getPost_name());
			if(countName>=1){
				throw new PostNotFondException("更新的职务名称已存在！");
			}
		}
		
		
		if("N".equals(postParam.getState())){
			int count = userChildAccountDao.findCountByPostNo(org_id,postParam.getId());
			if(count>=1){
				throw new PostNotFondException("该职务下还有人员在使用！");
			}
		}
		
		if(postParam.getOrder_no()!=null&&!(IntegerUtil.isNumeric(postParam.getOrder_no().toString()))){
			throw new PostNotFondException("排序类型为整型！");
		}
		
		String update_user_id = request.getSession().getAttribute("id").toString();	
//		String update_user_id = "1";
		if(update_user_id==null){
			throw new PostNotFondException("请登录！");
		}
		String format = "yyyy-MM-dd HH:mm:ss";
		String update_date = DateUtil.getNowDate(format);
		
		postParam.setUpdate_date(update_date);
		postParam.setUpdate_user_id(update_user_id);
		postParam.setOrg_id(org_id);
		
		int countAdd = postDao.updatePost(postParam);
		if(countAdd==0){
			throw new PostNotFondException("更新失败！");
		}
		
		return "更新成功！";
	}
	
	//删除岗位信息
	public String delPost(HttpServletRequest request, String id) throws PostNotFondException {
		if(id == null || id.trim().isEmpty()){
			throw new PostNotFondException("请选择需要删除的职务！");
		}
		
		if(!(IntegerUtil.isNumeric(id))){
			throw new PostNotFondException("职务的id为整型！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		int count = userChildAccountDao.findCountByPostNo(org_id,id);
		
		if(count>=1){
			throw new PostNotFondException("该职务下还有人员在使用！");
		}
		
		int del = postDao.delPost(id,org_id);
		if(del==0){
			throw new PostNotFondException("删除失败！");
		}
		
		return "删除成功！";
	}
	
	//查询所有的岗位信息 HttpServletRequest request, 
	public String findPostAllCount(HttpServletRequest request, FindListPostParam findListPostParam)
			throws PostNotFondException {
		String org_id = request.getSession().getAttribute("org_id").toString();
		findListPostParam.setOrg_id(org_id);
		findListPostParam.setState("Y");
		
		return postDao.findPostAllCount(findListPostParam);
	}
	
	

}
